<?php

namespace search\controllers\bigdata;

use business\elasticsearch\services\BigDataMohurdProjectService;

class MohurdProjectController extends BaseController
{
    public $enableCsrfValidation = false;//csrf安全

    /**
     * @api {get} /bigdata/mohurd-project/get-list 01、大数据在建项目搜索
     *
     * @apiVersion 1.0.0
     * @apiGroup bigdataMohurdProject
     *
     * @apiDescription 01、大数据在建项目搜索
     *
     * @apiParam {String} keyword                                            搜索词
     * @apiParam {String} province                                           省份
     * @apiParam {String} city                                               城市
     * @apiParam {String} company_type                                       企业类型
     * @apiParam {String} is_price_gt_avg                                    高端项目标签
     * @apiParam {String} prj_type_num                                       工程用途
     * @apiParam {String} prj_property_num                                   建设性质
     * @apiParam {String} all_invest_start/all_invest_end                    总投资
     * @apiParam {String} page                                               页码
     * @apiParam {String} pageSize                                           分页size
     *
     * @apiSuccess {Boolean} success                    返回状态
     * @apiSuccess {String}  message                    返回消息内容
     * @apiSuccess {Number}  errorCode                  错误码
     * @apiSuccess {Object}  data                       对象数组
     *
     */
    public function actionGetList()
    {
        $keyword = $this->getParam('keyword');
        $province = $this->getParam('province_code');
        $city = $this->getParam('city_code');
        $companyType = $this->getParam('company_type');//企业类型
        $isPriceGtAvg = $this->getParam('is_price_gt_avg');//高端项目
        $prjPropertyNum = $this->getParam('prj_property_num');//建筑性质
        $prjTypeNum = $this->getParam('prj_type_num');//工程用途
        $allInvestStart = $this->getParam('all_invest_start');//总投资
        $allInvestEnd = $this->getParam('all_invest_end');//总投资
        $startDateStart = $this->getParam('start_date_start');//立项、开工时间
        $startDateEnd = $this->getParam('start_date_end');//立项、开工时间
        $completeDateStart = $this->getParam('complete_date_start');//竣工时间
        $completeDateEnd = $this->getParam('complete_date_end');//竣工时间
        $area = $this->getParam('area');//项目地区
        $companyArea = $this->getParam('company_area');//企业地区
        $createdTime = $this->getParam('created_time');
        $organizingCode = $this->getParam('organizing_code');
        $page = $this->getParam('page');
        $pageSize = $this->getParam('page_size');
        $page = intval($page) > 0 ? intval($page) : 1;
        $pageSize = intval($pageSize) > 0 ? intval($pageSize) : 10;

        $province = array_filter(explode(",", $province));
        $city = array_filter(explode(",", $city));
        $area = array_filter(explode(",", $area));
        $companyArea = array_filter(explode(",", $companyArea));
        $companyType = array_filter(explode(",", $companyType));
        $prjPropertyNum = array_filter(explode(",", $prjPropertyNum));
        $prjTypeNum = array_filter(explode(",", $prjTypeNum));
        $isPriceGtAvg = array_filter(explode(",", $isPriceGtAvg));
        $keyword = array_filter(explode(",", $keyword));
        $organizingCode = array_filter(explode(",", $organizingCode));
        
        //企业标签
        $tags = [];
        $developer_top_100_label = $this->getParam('developer_top_100_label');
        if (!empty($developer_top_100_label)) {
            $tags['developer_top_100_label'] = $developer_top_100_label;
        }
        $newDecorateExpandLabel = $this->getParam('new_decorate_expand_label');
        if (!empty($newDecorateExpandLabel)) {
            $tags['new_decorate_expand_label'] = $newDecorateExpandLabel;
        }
        $newLandUsageExpandLabel = $this->getParam('new_land_usage_expand_label');
        if (!empty($newLandUsageExpandLabel)) {
            $tags['new_land_usage_expand_label'] = $newLandUsageExpandLabel;
        }
        $newAreaExpandLabel = $this->getParam('new_area_expand_label');
        if (!empty($newAreaExpandLabel)) {
            $tags['new_area_expand_label'] = $newAreaExpandLabel;
        }
        $priceGtAvgLabel = $this->getParam('price_gt_avg_label');
        if (!empty($priceGtAvgLabel)) {
            $tags['price_gt_avg_label'] = $priceGtAvgLabel;
        }
        $urbanInvestmentLabel = $this->getParam('urban_investment_label');
        if (!empty($urbanInvestmentLabel)) {
            $tags['urban_investment_label'] = $urbanInvestmentLabel;
        }

        try {
            $data = (new BigDataMohurdProjectService())->getDataList(
                $keyword,
                $province,
                $city,
                $area,
                $companyType,
                $companyArea,
                $isPriceGtAvg,
                $prjPropertyNum,
                $prjTypeNum,
                $allInvestStart,
                $allInvestEnd,
                $startDateStart,
                $startDateEnd,
                $completeDateStart,
                $completeDateEnd,
                $createdTime,
                $tags,
                $organizingCode,
                $page,
                $pageSize
            );

            return $this->success($data);
        } catch (\Exception $exception) {
            \Yii::error('找在建项目搜索失败:' . $exception->getMessage());
            return $this->error(-1, $exception->getMessage());
        }
    }

    /**
     * @api {get} /bigdata/mohurd-project/guess-names 02、在建项目联想
     *
     * @apiVersion 1.0.0
     * @apiGroup bigdataMohurdProject
     *
     * @apiDescription 02、在建项目联想
     *
     * @apiParam {String} keyword                   搜索词
     * @apiParam {String} pageSize                  分页size
     *
     * @apiSuccess {Boolean} success                    返回状态
     * @apiSuccess {String}  message                    返回消息内容
     * @apiSuccess {Number}  errorCode                  错误码
     * @apiSuccess {Object}  data                       对象数组
     * @apiSuccess {Object}  data.total
     * @apiSuccess {Object}  data.list
     * @apiSuccess {Number}  data.list.titleHL     搜索词高亮开工项目名
     * @apiSuccess {String}  data.list.title       开工项目
     *
     *
     */
    public function actionGuessNames()
    {
        try {
            $keyword = $this->getParam('keyword');
            $pageSize = $this->getParam('page_size');
            $pageSize = intval($pageSize) > 0 ? intval($pageSize) : 10;

            if (empty($keyword)) {
                throw new \Exception('你的关键字没有传');
            }
            $bigDataMohurdProjectService = new BigDataMohurdProjectService();
            $rs = $bigDataMohurdProjectService->guessNames($keyword, $pageSize);
            return $this->success($rs);

        } catch (\Exception $ex) {
            \Yii::error($ex->getMessage());
            return $this->error('', '获取失败，请稍后重试');
        }
    }
}